package com.lzq.online_learning_system.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author Administrator
 * @Date 2019/10/23 0023 8:49
 * @Version 1.0
 */
@Controller
public class UploadController {
    @RequestMapping(value = "/upload1",method = RequestMethod.POST)
    @ResponseBody
    public  String uploadFile(MultipartFile file, HttpServletRequest httpServletRequest){
        try {
            //创建文件在服务器端存放路径
     /*    String dir=   httpServletRequest.getServletContext().getRealPath("/load");*/
            /*String dir="D://teacher";*/
            String dir="E:\\";
            File filedir=new File(dir);
             if(!filedir.exists()) {
                filedir.mkdirs();
                //生成文件在服务器的名称
                String fileSuffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                String fileName =UUID.randomUUID().toString()+fileSuffix;
               File files=new File(filedir+"/"+fileName);
                //上传
                 file.transferTo(files);

             }
        }catch (Exception e){
            return "上传失败";
        }
     return "上传成功";
    }
    @RequestMapping("/uploa")
    public String upda(){
        return  "update";
    }

    /**
     *  图片上传功能
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file) {
        if (file.isEmpty()){ return "上传失败，请选择文件";}
        String filename = UUID.randomUUID()+file.getOriginalFilename();
        String filePath = "F:\\2019.6\\项目\\在线学习系统\\online_learning_system\\前端页面\\images\\";
        File dest = new File(filePath + filename);
        try {
            file.transferTo(dest);

            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
    }
    @RequestMapping("/multiUpload")
    @ResponseBody
    public  String multiUpload(HttpServletRequest request){ List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        String filePath = "F:\\2019.6\\项目\\在线学习系统\\online_learning_system\\前端页面\\images\\";
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            if (file.isEmpty()) {
                return "上传第" + (i++) + "个文件失败";
            }
            String fileName =UUID.randomUUID()+ file.getOriginalFilename();

            File dest = new File(filePath + fileName);
            try {
                file.transferTo(dest);

            } catch (IOException e) {

                return "上传第" + (i++) + "个文件失败";
            }
        }

        return "上传成功";




    }

}
